﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using Maticsoft.DBUtility;//Please add references
namespace EnterpriseFund.DAL
{
    /// <summary>
    /// 数据访问类:DJ_NSRXX_Extension1
    /// </summary>
    public partial class DJ_NSRXX_Extension1
    {
        public DJ_NSRXX_Extension1()
        { }
        #region  BasicMethod
        public bool Exists(decimal NSRDZDAH, decimal FYear, string FEntityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from fund.t_DJ_NSRXX_Extension1");
            strSql.Append(" where NSRDZDAH=@NSRDZDAH and FYear=@FYear and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@NSRDZDAH", SqlDbType.Decimal,13),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = NSRDZDAH;
            parameters[1].Value = FYear;
            parameters[2].Value = FEntityNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        public bool Add(EnterpriseFund.Model.DJ_NSRXX_Extension1 model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into fund.t_DJ_NSRXX_Extension1(");
            strSql.Append("NSRDZDAH,FFinancePropNo,FYear,FEnterprisePropNo,FEntityNo,FInsEmpNo,FInsDeptNo,FInsDate,FInsIP,FUpdEmplNo,FUpdDeptNo,FUpdDate,FUpdIP)");
            strSql.Append(" values (");
            strSql.Append("@NSRDZDAH,@FFinancePropNo,@FYear,@FEnterprisePropNo,@FEntityNo,@FInsEmpNo,@FInsDeptNo,@FInsDate,@FInsIP,@FUpdEmplNo,@FUpdDeptNo,@FUpdDate,@FUpdIP)");
            SqlParameter[] parameters = {
					new SqlParameter("@NSRDZDAH", SqlDbType.Decimal,13),
					new SqlParameter("@FFinancePropNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FInsDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FUpdDate", SqlDbType.VarChar,30),
					new SqlParameter("@FUpdIP", SqlDbType.VarChar,15)};
            parameters[0].Value = model.NSRDZDAH;
            parameters[1].Value = model.FFinancePropNo;
            parameters[2].Value = model.FYear;
            parameters[3].Value = model.FEnterprisePropNo;
            parameters[4].Value = model.FEntityNo;
            parameters[5].Value = model.FInsEmpNo;
            parameters[6].Value = model.FInsDeptNo;
            parameters[7].Value = model.FInsDate;
            parameters[8].Value = model.FInsIP;
            parameters[9].Value = model.FUpdEmplNo;
            parameters[10].Value = model.FUpdDeptNo;
            parameters[11].Value = model.FUpdDate;
            parameters[12].Value = model.FUpdIP;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool Update(EnterpriseFund.Model.DJ_NSRXX_Extension1 model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update fund.t_DJ_NSRXX_Extension1 set ");
            strSql.Append("FInsEmpNo=@FInsEmpNo,");
            strSql.Append("FInsDeptNo=@FInsDeptNo,");
            strSql.Append("FInsDate=@FInsDate,");
            strSql.Append("FInsIP=@FInsIP,");
            strSql.Append("FUpdEmplNo=@FUpdEmplNo,");
            strSql.Append("FUpdDeptNo=@FUpdDeptNo,");
            strSql.Append("FUpdDate=@FUpdDate,");
            strSql.Append("FUpdIP=@FUpdIP,");
            strSql.Append("FFinancePropNo=@FFinancePropNo,");
            strSql.Append("FEnterprisePropNo=@FEnterprisePropNo,");
            strSql.Append("FEntityNo=@FEntityNo"); 
            strSql.Append(" where NSRDZDAH=@NSRDZDAH and FYear=@FYear ");
            SqlParameter[] parameters = {
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FInsDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FUpdDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FUpdDate", SqlDbType.VarChar,30),
					new SqlParameter("@FUpdIP", SqlDbType.VarChar,15),
					new SqlParameter("@NSRDZDAH", SqlDbType.Decimal,13),
					new SqlParameter("@FFinancePropNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = model.FInsEmpNo;
            parameters[1].Value = model.FInsDeptNo;
            parameters[2].Value = model.FInsDate;
            parameters[3].Value = model.FInsIP;
            parameters[4].Value = model.FUpdEmplNo;
            parameters[5].Value = model.FUpdDeptNo;
            parameters[6].Value = model.FUpdDate;
            parameters[7].Value = model.FUpdIP;
            parameters[8].Value = model.NSRDZDAH;
            parameters[9].Value = model.FFinancePropNo;
            parameters[10].Value = model.FYear;
            parameters[11].Value = model.FEnterprisePropNo;
            parameters[12].Value = model.FEntityNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.DJ_NSRXX_Extension1 GetModel(decimal NSRDZDAH, decimal FYear, string FEntityNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 NSRDZDAH,FFinancePropNo,FYear,FEnterprisePropNo,FEntityNo,FInsEmpNo,FInsDeptNo,FInsDate,FInsIP,FUpdEmplNo,FUpdDeptNo,FUpdDate,FUpdIP from fund.t_DJ_NSRXX_Extension1 ");
            strSql.Append(" where NSRDZDAH=@NSRDZDAH and FYear=@FYear and FEntityNo=@FEntityNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@NSRDZDAH", SqlDbType.Decimal,13),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)			};
            parameters[0].Value = NSRDZDAH;
            parameters[1].Value = FYear;
            parameters[2].Value = FEntityNo;

            EnterpriseFund.Model.DJ_NSRXX_Extension1 model = new EnterpriseFund.Model.DJ_NSRXX_Extension1();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public string AddALL(List<Model.DJ_NSRXX_Extension1> model1List, List<Model.DJ_NSRXX_Extension1> updatelist)
        {
            StringBuilder strSql = new StringBuilder();
            SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString);
            conn.Open();
            SqlTransaction tran = conn.BeginTransaction();
            SqlCommand command = new SqlCommand();
            command.CommandTimeout = 60 * 60;
            string str = "";
            string errname = "";
            try
            {


                foreach (Model.DJ_NSRXX_Extension1 model1 in model1List)
                {
                    strSql = new StringBuilder();

                    strSql.Append("insert into fund.t_DJ_NSRXX_Extension1(");
                    strSql.Append("NSRDZDAH,FFinancePropNo,FYear,FEnterprisePropNo,FEntityNo,FInsEmpNo,FInsDeptNo,FInsDate,FInsIP)");
                    strSql.Append(" values (");
                    strSql.Append("@NSRDZDAH,@FFinancePropNo,@FYear,@FEnterprisePropNo,@FEntityNo,@FInsEmpNo,@FInsDeptNo,@FInsDate,@FInsIP)");
                    SqlParameter[] parameters = {
						new SqlParameter("@NSRDZDAH", SqlDbType.Decimal,13),
						new SqlParameter("@FFinancePropNo", SqlDbType.NVarChar,6),
						new SqlParameter("@FYear", SqlDbType.Decimal,5),
						new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6),
						new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
						new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
						new SqlParameter("@FInsDeptNo", SqlDbType.NVarChar,12),
						new SqlParameter("@FInsDate", SqlDbType.DateTime),
						new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
                                                };
                    parameters[0].Value = model1.NSRDZDAH;
                    parameters[1].Value = model1.FFinancePropNo;
                    parameters[2].Value = model1.FYear;
                    parameters[3].Value = model1.FEnterprisePropNo;
                    parameters[4].Value = model1.FEntityNo;
                    parameters[5].Value = model1.FInsEmpNo;
                    parameters[6].Value = model1.FInsDeptNo;
                    parameters[7].Value = model1.FInsDate;
                    parameters[8].Value = model1.FInsIP;
                   
                    command = new SqlCommand(strSql.ToString(), conn, tran);
                    command.Parameters.AddRange(parameters);
                    command.CommandTimeout = 60 * 60;
                    command.ExecuteNonQuery();

                    errname = model1.NSRDZDAH.ToString();

                }
                foreach (Model.DJ_NSRXX_Extension1 model in updatelist)
                {
                    strSql = new StringBuilder();
                    strSql.Append("update fund.t_DJ_NSRXX_Extension1 set ");
                    strSql.Append("FInsEmpNo=@FInsEmpNo,");
                    strSql.Append("FInsDeptNo=@FInsDeptNo,");
                    strSql.Append("FInsDate=@FInsDate,");
                    strSql.Append("FInsIP=@FInsIP,");
                   
                    strSql.Append("FFinancePropNo=@FFinancePropNo,");
                    strSql.Append("FEnterprisePropNo=@FEnterprisePropNo,");
                    strSql.Append("FEntityNo=@FEntityNo");
                    strSql.Append(" where NSRDZDAH=@NSRDZDAH and FYear=@FYear ");
                    SqlParameter[] parameters = {
					new SqlParameter("@FInsEmpNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FInsDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15),
										new SqlParameter("@NSRDZDAH", SqlDbType.Decimal,13),
					new SqlParameter("@FFinancePropNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
                    parameters[0].Value = model.FInsEmpNo;
                    parameters[1].Value = model.FInsDeptNo;
                    parameters[2].Value = model.FInsDate;
                    parameters[3].Value = model.FInsIP;
                  
                    parameters[4].Value = model.NSRDZDAH;
                    parameters[5].Value = model.FFinancePropNo;
                    parameters[6].Value = model.FYear;
                    parameters[7].Value = model.FEnterprisePropNo;
                    parameters[8].Value = model.FEntityNo;

                    command = new SqlCommand(strSql.ToString(), conn, tran);
                    command.CommandTimeout = 60 * 60;
                    command.Parameters.AddRange(parameters);
                    command.ExecuteNonQuery();
                    errname = model.NSRDZDAH.ToString();
                }
                tran.Commit();

                return str;

            }
            catch (Exception ex)
            {
                tran.Rollback();
                str = "导入异常" + errname + ex.Message;

                return str;
            }
            finally
            {
                conn.Close();
            }

        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.DJ_NSRXX_Extension1 DataRowToModel(DataRow row)
        {
            EnterpriseFund.Model.DJ_NSRXX_Extension1 model = new EnterpriseFund.Model.DJ_NSRXX_Extension1();
            if (row != null)
            {
                if (row["NSRDZDAH"] != null && row["NSRDZDAH"].ToString() != "")
                {
                    model.NSRDZDAH = decimal.Parse(row["NSRDZDAH"].ToString());
                }
                if (row["FFinancePropNo"] != null)
                {
                    model.FFinancePropNo = row["FFinancePropNo"].ToString();
                }
                if (row["FYear"] != null && row["FYear"].ToString() != "")
                {
                    model.FYear = decimal.Parse(row["FYear"].ToString());
                }
                if (row["FEnterprisePropNo"] != null)
                {
                    model.FEnterprisePropNo = row["FEnterprisePropNo"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FInsEmpNo"] != null)
                {
                    model.FInsEmpNo = row["FInsEmpNo"].ToString();
                }
                if (row["FInsDeptNo"] != null)
                {
                    model.FInsDeptNo = row["FInsDeptNo"].ToString();
                }
                if (row["FInsDate"] != null && row["FInsDate"].ToString() != "")
                {
                    model.FInsDate = DateTime.Parse(row["FInsDate"].ToString());
                }
                if (row["FInsIP"] != null)
                {
                    model.FInsIP = row["FInsIP"].ToString();
                }
                if (row["FUpdEmplNo"] != null)
                {
                    model.FUpdEmplNo = row["FUpdEmplNo"].ToString();
                }
                if (row["FUpdDeptNo"] != null)
                {
                    model.FUpdDeptNo = row["FUpdDeptNo"].ToString();
                }
                if (row["FUpdDate"] != null && row["FUpdDate"].ToString() != "")
                {
                    model.FUpdDate = DateTime.Parse(row["FUpdDate"].ToString());
                }
                if (row["FUpdIP"] != null)
                {
                    model.FUpdIP = row["FUpdIP"].ToString();
                }
            }
            return model;
        }

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

